package com.summer.shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.shopping.domain.table.User;
import com.summer.shopping.domain.table.dto.UserLoginDTO;
import com.summer.shopping.domain.table.dto.UserRegistDTO;
import com.summer.shopping.service.UserService;
import com.summer.shopping.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import java.time.LocalDateTime;

/**
* @author 86136
* @description 针对表【user(用户信息表)】的数据库操作Service实现
* @createDate 2025-06-20 13:57:19
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    UserMapper userMapper;
    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String password = DigestUtils.md5DigestAsHex(userLoginDTO.getPassword().getBytes());
        LambdaQueryWrapper<User> userWrapper=new LambdaQueryWrapper<>();
        userWrapper.eq(User::getUsername,userLoginDTO.getUsername());
        userWrapper.eq(User::getPassword,password);
        User user = userMapper.selectOne(userWrapper);
        return user;
    }

    @Override
    public Integer userRegist(UserRegistDTO userRegistDTO) {
        User user=new User();
        BeanUtils.copyProperties(userRegistDTO,user);
        user.setPassword(DigestUtils.md5DigestAsHex(userRegistDTO.getPassword().getBytes()));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.insert(user);
    }
}




